package com.nus.fellowmark.modules.rbac.controller;

import java.util.Arrays;
import java.util.Map;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.SysUserRoleMappingEntity;
import com.nus.fellowmark.modules.rbac.dto.UserRoleDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nus.fellowmark.entity.SysUserRoleMappingEntity;
import com.nus.fellowmark.modules.rbac.service.SysUserRoleMappingService;
import com.nus.fellowmark.common.utils.PageUtils;



/**
 * User-Role Relationship Table
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-11  
 */
@RestController
@RequestMapping("sys_user_role_mapping")
public class SysUserRoleMappingController {
    @Autowired
    private SysUserRoleMappingService sysUserRoleMappingService;



    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("List system userRoleMapping")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = sysUserRoleMappingService.queryPage(params);
        return ServerResponseEntity.success(page);
    }


    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("select userRoleMapping by id")
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Integer id){
        SysUserRoleMappingEntity userRoleMapping = sysUserRoleMappingService.getById(id);

        return ServerResponseEntity.success("userRoleMapping", userRoleMapping);
    }

    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("save userRoleMapping")
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody UserRoleDTO userRoleDTO){
        Boolean result = sysUserRoleMappingService.save(userRoleDTO);
        if (!result) {
            return ServerResponseEntity.showFailMsg("save user role failed");
        }

        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("update userRoleMapping")
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody UserRoleDTO userRoleDTO){
        Boolean result = sysUserRoleMappingService.updateById(userRoleDTO);
        if (!result) {
            return ServerResponseEntity.showFailMsg("update user role failed");
        }

        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("delete userRoleMapping")
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Integer[] ids){
        sysUserRoleMappingService.removeByIds(Arrays.asList(ids));

        return ServerResponseEntity.success();
    }
}
